#include "Acceptor.h"
#include "TcpConnection.h"
#include "EventLoop.h"
#include <iostream>
#include <string>
using std::cout;
using std::endl;
using std::string;

void onNewConnection(const TcpConnection::TcpConnectionPtr &con){
    cout << con->toString() << " has connected!!!" << endl;
}

void onMessage(const TcpConnection::TcpConnectionPtr &con){
    string msg = con->receive();
    cout << ">>>>recv from client : " << msg << endl;
    con->send("对不起，做不到!\n");
}

void onClose(const TcpConnection::TcpConnectionPtr &con){
    cout << con->toString() << " has closed!!!" << endl;
}


int main() {
    Acceptor acceptor("127.0.0.1", 8888);
    acceptor.ready();//此时处于监听状态

    EventLoop eventLoop(acceptor);

    /**
     * 分析1:如果EventLoop的set用左值，则无法传参，需要const左值，所以必须用右值
     * 分析2:如果EventLoop和TcpConnection都用右值，则EventLoop的函数对象在第一次连接后置为空，
     *      后续连接无法连上，并且，peek一直会有值，读不出来，所以会疯狂打印
     * 结论:EventLoop传右值，TcpConnection传左值
     */
    eventLoop.setOnNewConnectionCallback(std::move(onNewConnection));
    eventLoop.setOnMessageCallback(std::move(onMessage));
    eventLoop.setOnCloseCallback(std::move(onClose));

    eventLoop.loop();
    return 0;
}
